package com.wanshi.utils;

import com.wanshi.exception.MyException;
import java.io.*;

@SuppressWarnings("all")
public class DaTiUtils<T>{

    public T writeContent(String uuid,String content,
                          String method,Object[] objs) throws Exception {
        String name = "test"+uuid;
        File fileJava = null;
        File fileClass = null;
        try{

            String path = this.getClass().getResource("/").getPath();
            fileJava = new File(path,name+".java");
            if(!fileJava.exists()){
                //1 写入
                FileWriter fw = new FileWriter(fileJava);
                fw.write("import java.io.*;\n" +
                        "import java.util.*;\n" +
                        "public class "+name+" {\n");

                fw.write(content);
                fw.write("\n}");
                fw.close();
            }

            fileClass = new File(path,name+".class");
            if(!fileClass.exists()){
                //2 编译
                Runtime run =Runtime.getRuntime();
                Process process = run.exec("javac " + fileJava.getAbsolutePath());
                process.waitFor();
                process.destroy();
            }
            if(!fileClass.exists()){
                throw new MyException("答案编译错误!");
            }
            DatiThread<T> t1 = new DatiThread(name,method,objs);
            t1.start();
            // 主线程中 执行的 t1.join(500);
            // 主线程最多会等待 t1线程500毫秒,如果500毫秒之内t1线程执行完毕,主线程直接往下执行;
            // 如果等够500毫秒, t1线程还没有结束,主线程也会往下执行,
            // 主要解决的就是 用户代码死循环的问题
            t1.join(500);
            Object result = t1.getResult();
            if(result == null){
                t1.stop();
                return null;
            }
            return t1.getResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }



    public String writeContent2(String uuid,String content,String method,Object[] objs) throws Exception {
        //String uuid = UUID.randomUUID().toString().replace("-","");
        String name = "test"+uuid;
        File fileJava = null;
        File fileClass = null;
        try{
            String path = this.getClass().getResource("/").getPath();
            fileJava = new File(path,name+".java");
            if(!fileJava.exists()){
                //1 写入
                FileWriter fw = new FileWriter(fileJava);
                fw.write("import java.io.*;\n" +
                        "import java.util.*;\n" +
                        "public class "+name+" {\n");

                fw.write(content);
                fw.write("\n}");
                fw.close();
            }

            fileClass = new File(path,name+".class");
            if(!fileClass.exists()){
                //2 编译
                Runtime run =Runtime.getRuntime();
                Process process = run.exec("javac " + fileJava.getAbsolutePath());
                process.waitFor();
                process.destroy();
            }
            if(!fileClass.exists()){
                throw new MyException("答案编译错误!");
            }

            DatiOutThread t1 = new DatiOutThread(name,method,objs);
            t1.start();
            t1.join(500);
            Object result = t1.getResult();
            if(result == null){
                t1.stop();
                return null;
            }
            return t1.getResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    public static void deleteFile(String uuid) {
        String name = "test"+uuid;
        File fileJava = null;
        File fileClass = null;
        String path = DaTiUtils.class.getResource("/").getPath();
        fileJava = new File(path,name+".java");
        if(fileJava != null && fileJava.exists()){
            fileJava.delete();
        }
        fileClass = new File(path,name+".class");
        if(fileClass != null && fileClass.exists()){
            fileClass.delete();
        }
    }
}
